package com.chiga.action.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.chiga.action.AbstractAction;
import com.chiga.annotation.Description;
import com.chiga.support.GeneratorSupport;

@Controller
public class CodeGeneratorAction extends AbstractAction {

	private static final long serialVersionUID = 514390363076613020L;
	
	private String className;
	private String savePath = "/workspaces/Opteron/src/";

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String listClass() {
		this.pushData("clazz", GeneratorSupport.listModel());
		return SUCCESS;
	}
	
	public String listFields() {
		try {
			
			Class<?> clazz = Class.forName(className);
			Description des = clazz.getAnnotation(Description.class);
			if(des != null) {
				this.pushData("table", des.value());
			}
			
			for(Field field:clazz.getDeclaredFields()) {
				des = field.getAnnotation(Description.class);
				if(des == null) continue;
				this.pushData(field.getName(), des.value());
			}
			
		} catch (ClassNotFoundException e) {
			this.error("無此Class", e);
		}
		
		return SUCCESS;
	}
	
	public String generateDao() {
		try {
			this.pushData(GeneratorSupport.generateDao(className));
		} catch (ClassNotFoundException e) {
			this.error("無此Class", e);
		}
		
		return SUCCESS;
	}
	
	public String saveDaoFile() {
		Map<String, String> map;
		try {
			map = GeneratorSupport.generateDao(className);
		} catch (ClassNotFoundException e) {
			return this.error("無此Class", e);
		}
		String path = savePath+(map.get("interfacePackage").replaceAll("\\.", "/")+"/"+map.get("interfaceName")+".java");
		
		try {
			File file = new File(path);
			if(!file.exists()){
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			}else{
				return this.warn("已有檔案");
			}
			FileWriter writer = new FileWriter(file);
			writer.write(map.get("interfaceContent"));
			writer.close();
		} catch (IOException e) {
			return this.error("",e);
		}
		
		path = savePath+(map.get("implementPackage").replaceAll("\\.", "/")+"/"+map.get("implementName")+".java");
		
		try {
			File file = new File(path);
			if(!file.exists()){
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			}else{
				return this.warn("已有檔案");
			}
			FileWriter writer = new FileWriter(file);
			writer.write(map.get("implementContent"));
			writer.close();
		} catch (IOException e) {
			return this.error("",e);
		}
		
		this.info("存檔完成");
		return SUCCESS;
	}
	
	public String generateService() {
		try {
			this.pushData(GeneratorSupport.generateService(className));
		} catch (ClassNotFoundException e) {
			return this.error("無此Class", e);
		}
		
		return SUCCESS;
	}
	
	public String saveServiceFile() {
		Map<String, String> map;
		try {
			map = GeneratorSupport.generateService(className);
		} catch (ClassNotFoundException e) {
			this.error("無此Class", e);
			return SUCCESS;
		}
		String path = savePath+(map.get("interfacePackage").replaceAll("\\.", "/")+"/"+map.get("interfaceName")+".java");
		
		try {
			File file = new File(path);
			if(!file.exists()) {
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			} else {
				return this.warn("已有檔案");
			}
			FileWriter writer = new FileWriter(file);
			writer.write(map.get("interfaceContent"));
			writer.close();
		} catch (IOException e) {
			return this.error("",e);
		}
		
		path = savePath+(map.get("implementPackage").replaceAll("\\.", "/")+"/"+map.get("implementName")+".java");
		
		try {
			File file = new File(path);
			if(!file.exists()) {
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			} else {
				return this.warn("已有檔案");
			}
			FileWriter writer = new FileWriter(file);
			writer.write(map.get("implementContent"));
			writer.close();
		} catch (IOException e) {
			return this.error("",e);
		}
		
		this.info("存檔完成");
		return SUCCESS;
	}
	
	public String generateAction() {
		try {
			this.pushData(GeneratorSupport.generateAction(className));
		} catch (ClassNotFoundException e) {
			this.error("無此Class", e);
		}
		return SUCCESS;
	}
	
	public String saveActionFile() {
		Map<String, String> map;
		try {
			map = GeneratorSupport.generateAction(className);
		} catch (ClassNotFoundException e) {
			return this.error("無此Class", e);
		}
		String path = savePath+(map.get("actionPackage").replaceAll("\\.", "/")+"/"+map.get("actionName")+".java");
		
		try {
			File file = new File(path);
			if(!file.exists()) {
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			} else {
				return this.warn("已有檔案");
			}
			FileWriter writer = new FileWriter(file);
			writer.write(map.get("actionContent"));
			writer.close();
		} catch (IOException e) {
			return this.error("",e);
		}
		
		this.info("存檔完成");
		return SUCCESS;
	}
}
